package com.xiaoyu.lanling.base.push.vivo;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.xiaoyu.base.push.BasePushAgent;
import com.xiaoyu.base.push.PushConstants;
import com.xiaoyu.lanling.base.push.alternate.PushAlternateAgent;


import in.srain.cube.util.CLog;

public class VIVOPushAgent extends BasePushAgent {

    private static VIVOPushAgent sInstance;
    private Context mContext;
    private PushAlternateAgent.OnPushReceiver mOnPushReceiver;

    public VIVOPushAgent() {
        super(PushConstants.PUSH_TYPE_VIVO);
    }

    public static synchronized VIVOPushAgent getInstance() {
        if (sInstance == null) {
            sInstance = new VIVOPushAgent();
        }
        return sInstance;
    }

    @Override
    protected void tryToStartPushService(final Context context) {
        mContext = context;
        super.tryToStartPushService(context);
        if (!PushClient.getInstance(context).isSupport()) {
            CLog.e(TAG, "VIVO isSupportPush: " + false);
            onRegisterSuccess("unknown");
            tryToStartAlternatePushService();
            return;
        }
        PushClient.getInstance(context).initialize();
        PushClient.getInstance(context).turnOnPush(new IPushActionListener() {
            @Override
            public void onStateChanged(int i) {
                Log.d("lib-push", "code   " + i);
                onReceiveRegisterResult(i, PushClient.getInstance(context).getRegId());
            }
        });
    }

    public void onReceiveRegisterResult(int code, String registerId) {
        CLog.i(TAG, "onReceiveRegisterResult code %s，token %s", code, registerId);
        if (code == 0) {
            onRegisterSuccess(registerId);
            tryToStartAlternatePushService();
        } else {
            retryOnFail(String.valueOf(code));
        }
    }

    private void tryToStartAlternatePushService() {
        if (mOnPushReceiver == null) {
            mOnPushReceiver = new PushAlternateAgent.OnPushReceiver() {
                @Override
                public void onPushToken(String type, String token) {
                    onRegisterAlternateSuccess(type, token);
                }

                @Override
                public void onPushError(String errorMessage) {
                    waitingAlternateRegister();
                    addGetTokenEventLog("register_alternate_result", String.valueOf(errorMessage));
                    CLog.i(TAG, errorMessage);
                }
            };
        }
        PushAlternateAgent.getInstance().setOnPushReceiver(mOnPushReceiver);
        PushAlternateAgent.getInstance().startPushServer(mContext);
    }

    private void waitingAlternateRegister() {
        CLog.i(TAG, "waitingAlternateRegister");
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tryToStartAlternatePushService();
            }
        }, 180000);
    }
}
